package com.example.yuejian.yuejian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.base.BaseActivity;
import com.example.yuejian.yuejian.base.ModleListener;
import com.example.yuejian.yuejian.bean.ActivityInfo;
import com.example.yuejian.yuejian.bean.Collection;
import com.example.yuejian.yuejian.bean.IsFollow;
import com.example.yuejian.yuejian.model.ActivityModel;
import com.example.yuejian.yuejian.model.CollectionModel;
import com.squareup.picasso.Picasso;

public class ActivityDetailActivity extends BaseActivity {
     int activity_id;
    private ImageView img_photo;
    private TextView tv_activity_name;
    private TextView tv_activity_publishtime;
    private TextView tv_activity_address;
    private TextView tv_activity_starttime;
    private TextView tv_activity_money;
    private TextView tv_activity_peoplenum;
    private TextView tv_activity_phone;
    private TextView tv_activity_intro;
    private Button btn_sign_up;
    private ImageView btn_back;
    private ImageView btn_collect;
    ActivityInfo res;
    private ModleListener<ActivityInfo> listener =new ModleListener<ActivityInfo>() {
        @Override
        public void onResponse(ActivityInfo o) {
             res=o;
            tv_activity_name.setText(res.getActivityname());
            tv_activity_publishtime.setText(res.getCreatetime());
            tv_activity_address.setText(res.getPlace());
            tv_activity_starttime.setText(res.getTime());
            tv_activity_money.setText(res.getMoney()+"/人");
            tv_activity_peoplenum.setText(res.getNopeople());
            tv_activity_phone.setText(res.getPhone());
            tv_activity_intro.setText(res.getIntro());



            if (!"".equals(res.getPhoto()))
            {
                Picasso.with(getApplication()).load(res.getPhoto()).into(img_photo);
            }


        }

        @Override
        public void onFail(String msg) {

        }
    };
    private ModleListener<Collection>collectlistener=new ModleListener<Collection>() {
        @Override
        public void onResponse(Collection o) {
            Collection res=o;
            int stutas=res.getRetCode();
            if (1==stutas)
            {
                btn_collect.setImageResource(R.drawable.ic_myfollow);
                Toast.makeText(ActivityDetailActivity.this, res.getRetMsg(), Toast.LENGTH_SHORT).show();
            }
            else
            {
                btn_collect.setImageResource(R.drawable.ic_follow);
                Toast.makeText(ActivityDetailActivity.this, res.getRetMsg(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ActivityDetailActivity.this, "cuowu", Toast.LENGTH_SHORT).show();
        }
    };
    private ModleListener<IsFollow> isfollowlistener=new ModleListener<IsFollow>() {
        @Override
        public void onResponse(IsFollow o) {
            IsFollow res=o;
            int stutas=res.getRetCode();
            if (1==stutas)
            {
                btn_collect.setImageResource(R.drawable.ic_follow);
            }
            else
            {
                btn_collect.setImageResource(R.drawable.ic_myfollow);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ActivityDetailActivity.this, "cuowu", Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        initViews();
        getData();
        back();
        isFollow();

        initEvents();
    }
    private void initViews()
    {
        img_photo=(ImageView)findViewById(R.id.activity_detail_img);
        tv_activity_name=(TextView)findViewById(R.id.activity_detail_activityName);
        tv_activity_publishtime=(TextView)findViewById(R.id.activity_detail_publish_time);
        tv_activity_address=(TextView)findViewById(R.id.activity_detail_address);
        tv_activity_starttime=(TextView)findViewById(R.id.activity_detail_activity_startTime);
        tv_activity_money=(TextView)findViewById(R.id.activity_detail_activity_money);
        tv_activity_peoplenum=(TextView)findViewById(R.id.activity_detail_people_num);
        tv_activity_phone=(TextView)findViewById(R.id.activity_detail_phone);
        tv_activity_intro=(TextView)findViewById(R.id.activity_detail_intro);
        btn_sign_up=(Button)findViewById(R.id.btn_activity_detail_sign_up);
        btn_collect=(ImageView)findViewById(R.id.activity_detail_collect_img_btn);
    }
    private void getData()
    {

        Intent intent=getIntent();
        activity_id=  Integer.parseInt(intent.getStringExtra("activity_id")) ;
        ActivityModel model=new ActivityModel();
        model.ActivityInfo(activity_id,listener);
    }
    private void back()
    {
        btn_back=(ImageView)findViewById(R.id.imgBtn_activity_detail_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
    private  void initEvents()
    {
        btn_sign_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(ActivityDetailActivity.this,ActivitySignActivity.class);
                intent.putExtra("activity_name",res.getActivityname());
                intent.putExtra("money",res.getMoney()+"");
                intent.putExtra("aid",res.getId()+"");
                startActivity(intent);
            }
        });
        btn_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doFollow();
            }
        });
    }
    private void isFollow()
    {
        int collection_id=activity_id;
        //int user_id=Integer.parseInt(getUser_id());
        CollectionModel collectionModel=new CollectionModel();
        collectionModel.isfollow(getUser_id(),1,collection_id,isfollowlistener);

    }
    private  void doFollow()
    {
        int collection_id=activity_id;
        int user_id=Integer.parseInt(getUser_id());
        CollectionModel collectionModel=new CollectionModel();
        collectionModel.followMaster(user_id,1,collection_id,collectlistener);

    }
}
